<template>
	<!-- 新增生产计划 -->
	<div>
		<el-form ref="form" :model="form" :rules="rules" label-width="100px" :disabled="disabled">
			<el-row :gutter="20">
				<el-col :span="8">
					<el-form-item label="产品型号">
						<el-select v-model="curmodel" placeholder="请选择" @change="changeModel">
							<el-option v-for="item in productModelList" :key="item.productModel" :label="item.productModel" :value="item.productModel"> </el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="产品名称" prop="productName">
						<el-input v-model="curmodelobj.productName" disabled />
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="数量" prop="productNumber">
						<el-input v-model="form.productNumber" disabled />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="24">
					<el-form-item label="返修任务单" prop="repairNumber">
						<el-input v-model="form.repairNumber" disabled />
					</el-form-item>
				</el-col>
			</el-row>
			<div class="add-plan-title"><span class="title-text">生产计划</span></div>
			<div class="add-plan-box">
				<el-form-item label="总完成天数" prop="completionDays">
					<el-input-number v-model="form.completionDays" :min="1" :max="999999" :precision="0" />
				</el-form-item>
			</div>
			<div style="width: 100%">
				<el-table :data="curTechnologyList">
					<el-table-column prop="technologyName" label="工艺流程" min-width="180"> </el-table-column>
					<el-table-column prop="completionDays" label="完成天数" min-width="180">
						<template slot-scope="scope">
							<el-input-number v-model="scope.row.completionDays" style="100%" :min="1" :max="999999" :precision="0" />
						</template>
					</el-table-column>
				</el-table>
			</div>
		</el-form>
	</div>
</template>
<script>
import { dictList } from "@/api/selectList"
export default {
	name: "ProductPlan",
	dicts: ["repair_time"],
	props: {
		form: {
			type: Object,
			required: true,
			default: {}
		},
		openApprove: {
			type: Boolean,
			default: false
		},
		productTechnologyList: {
			type: Object,
			default: []
		},
		productModelList: {
			type: Object,
			default: []
		},
		disabled: {
			type: Boolean,
			default: false
		}
	},
	data() {
		return {
			// 表单校验
			rules: {
				approvalStatus: [{ required: true, message: "审批状态不能为空", trigger: "blur" }],
				causeRejection: [{ required: true, message: "驳回原因不能为空", trigger: "blur" }]
			},
			rules2: {
				approvalStatus: [{ required: true, message: "审批状态不能为空", trigger: "blur" }]
			},
			curmodel: undefined,
			curmodelobj: {},
			curTechnologyList: []
		}
	},
	watch: {
		openApprove: {
			handler(newVal, oldVal) {
				if (newVal) {
					this.curmodel = undefined
					this.curmodelobj = {}
					dictList({
						dictType: "repair_time"
					}).then(res => {
						this.form.completionDays = res.rows[0].dictLabel
					})
					this.curTechnologyList = []
				}
			},
			immediate: true
		}
	},
	methods: {
		// 更改型号
		changeModel() {
			this.curmodelobj = this.productModelList[this.curmodel]

			console.log(this.productModelList)
			console.log(this.curmodel)
			console.log(this.curmodelobj)
			this.curTechnologyList = this.productTechnologyList[this.curmodel]
		}

		// 计算总的计划完成时间
		// calEndTimeAll() {
		//   let time;
		//   if (this.form.programBeginTime) {
		//     time = new Date(this.form.programBeginTime);
		//   } else {
		//     // 默认开始时间是今天
		//     time = new Date();
		//     this.form.programBeginTime = parseTime(time, "{y}-{m}-{d}");
		//   }
		//   time.setDate(time.getDate() + Number(this.form.completionDays));
		//   this.form.programFinishTime = parseTime(time, "{y}-{m}-{d}");
		//   // 更改分步第一条时间
		//   if (this.form.tproductTechnologyList.length > 0) {
		//     this.form.tproductTechnologyList[0].technologyBeginTime = this.form.programBeginTime;
		//     this.calStepTime(this.form.tproductTechnologyList[0], 0);
		//   }
		// },
		// 计算分步的时间
		// calStepTime(row, index) {
		//   let time = new Date(row.technologyBeginTime);
		//   time.setDate(time.getDate() + Number(row.completionDays));
		//   row.technologyFinishTime = parseTime(time, "{y}-{m}-{d}");
		//   // 更改下一条的时间
		//   if (this.form.tproductTechnologyList[index + 1]) {
		//     // 下一条开始时间是上一条姐说时间+1天
		//     let time1 = new Date(row.technologyFinishTime);
		//     time1.setDate(time1.getDate() + 1);
		//     this.form.tproductTechnologyList[index + 1].technologyBeginTime = parseTime(
		//       time1,
		//       "{y}-{m}-{d}"
		//     );
		//     // 计算下一条的时间
		//     this.calStepTime(this.form.tproductTechnologyList[index + 1], index + 1);
		//   }
		// },
	}
}
</script>
<style scoped lang="scss">
.add-plan-title {
	width: 100%;
	height: 3rem;
	padding-left: 1rem;
}
.title-text {
	padding-bottom: 0.8rem;
	border-bottom: 2px solid rgb(42, 130, 228);
}
.add-plan-box {
	width: 100%;
	display: flex;
	flex-wrap: wrap;
	justify-content: space-between;
	> div {
		width: 48%;
	}
}
</style>
